package edu.bu.cs673.superkids123.parents.controller;

import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;

import edu.bu.cs673.superkids123.AbstractController;
import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.children.model.resources.AudioPlayer;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.children.model.resources.ResourceFormat;
import edu.bu.cs673.superkids123.children.model.resources.ResourceType;
import edu.bu.cs673.superkids123.common.DataObjectAccess.ChildManager;
import edu.bu.cs673.superkids123.common.model.Child;
import edu.bu.cs673.superkids123.common.view.LoginView;

import edu.bu.cs673.superkids123.parents.view.ParentSettingsView;

public class CustomizationController extends AbstractController {

	private static final int MAX_SAMPLE_IMAGES = 6;
	private static final float IMAGE_SCALE = 0.75f;
	private ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
	
	
	public CustomizationController(AbstractFrame mainFrame) {
		super(mainFrame);
	}
	
	public List<Child> getChildren() {
		return ChildManager.getChildren();
	}

	/**
	 * Returns sample images from the passed resource type
	 * 
	 * @param type
	 * @return array of sample images
	 */
	public ImageIcon[] getSampleImages(ResourceType type) {
		
		
		ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
		provider.setImagePolicy(type);
		
		ImageIcon[] icons = new ImageIcon[MAX_SAMPLE_IMAGES];
		for (int i = 0; i < MAX_SAMPLE_IMAGES; i++) {
			icons[i] = provider.getScaledImage(ChildrenResourceProvider.NUMBER_IMAGE_MAP.get(i), IMAGE_SCALE);
		}
		
		return icons;
	}
	
	/**
	 * Changes the current audio policy for previewing
	 * 
	 * @param ResourceType
	 */
	public void updateAudioPolicy(ResourceType type) {
		provider.setAudioPolicy(type);
	}
	
	public void submitUpdate(Child child,String imageResource,String audioResource){
		child.setImageResource(ResourceType.valueOf(imageResource));
		child.setAudioResource(ResourceType.valueOf(audioResource));
		
		// TODO: Save changes by calling update child in Child Manager
		ChildManager.updateChild(child);
		getMainFrame().changeView(ParentSettingsView.class);
		
	}
	
	/**
	 * Returns a list of String that are the resource files that either
	 * contain only images or images and audio
	 * 
	 * @return List<String> of enum ResourceType
	 */
	public List<String> getAvailableImagesList() {
		List<String> list = new ArrayList<String>();
		
		for (ResourceType type : ResourceType.values()) {
			ResourceFormat format = provider.getResourceFormat(type);
			if (format == ResourceFormat.Images || format == ResourceFormat.Both)
				list.add(type.name());
		}
		
		return list;
	}
	
	/**
	 * Returns a list of String that are the resource files that either
	 * contain only audio or images and audio
	 * 
	 * @return List<String> of enum ResourceType
	 */
	public List<String> getAvailableAudioList() {
		List<String> list = new ArrayList<String>();
		
		for (ResourceType type : ResourceType.values()) {
			ResourceFormat format = provider.getResourceFormat(type);
			if (format == ResourceFormat.Audio || format == ResourceFormat.Both)
				list.add(type.name());
		}
		
		return list;
	}
	
	/**
	 * For previewing Audio
	 * @param number
	 */
	public void playAudio(int number) {
        AudioPlayer audioPlayer = new AudioPlayer();
        byte[] audioData = provider.getAudioContentByNumber(number); 
        audioPlayer.playAudio(audioData);
        try {
                Thread.sleep(100);
        }
        catch (InterruptedException ie) {
                System.out.println("Thread interrupted");
        }
	}
}
